<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import {
  ElMessage,
  ElForm,
  ElFormItem,
  ElInput,
  ElButton,
  ElCheckbox,
  ElLink
} from 'element-plus'
import { useUserStore } from '@/stores/user'
import type { LoginForm } from '@/types/user'

const router = useRouter()
const userStore = useUserStore()

const loginForm = reactive<LoginForm>({
  username: '',
  password: '',
  remember: false
})

const loginRef = ref<InstanceType<typeof ElForm>>()
const loading = ref(false)

/* 登录验证规则 */
const loginRules = {
  username: [
    { required: true, trigger: 'blur', message: '请输入用户名' }
  ],
  password: [
    { required: true, trigger: 'blur', message: '请输入密码' }
  ]
}

/* 处理登录 */
const handleLogin = async () => {
  if (!loginRef.value) return

  await loginRef.value.validate(async (valid: boolean) => {
    if (!valid) return

    loading.value = true
    try {
      const result = await userStore.login({
        username: loginForm.username,
        password: loginForm.password
      })

      if (result.success) {
        ElMessage.success(result.message)
        router.push('/home')
      } else {
        ElMessage.error(result.message)
      }
    } catch (error) {
      ElMessage.error('登录失败，请稍后重试')
    } finally {
      loading.value = false
    }
  })
}

/* 处理注册 */
const handleRegister = () => {
  router.push('/register')
}

/* 处理忘记密码 */
const handleForgotPassword = () => {
  ElMessage.info('忘记密码功能暂未开放')
}
</script>

<template>
  <div class="login">
    <el-form
      ref="loginRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h3 class="title">企业数字化平台管理系统</h3>

      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          size="large"
          auto-complete="off"
          placeholder="请输入用户名"
          prefix-icon="User"
          @keyup.enter="handleLogin"
        />
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          size="large"
          auto-complete="off"
          placeholder="请输入密码"
          prefix-icon="Lock"
          show-password
          @keyup.enter="handleLogin"
        />
      </el-form-item>

      <div class="login-options">
        <el-checkbox v-model="loginForm.remember">
          记住密码
        </el-checkbox>
        <el-link type="primary" :underline="false" @click="handleForgotPassword">
          忘记密码？
        </el-link>
      </div>

      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="large"
          type="primary"
          style="width:100%;"
          @click="handleLogin"
        >
          <span>{{ loading ? '登录中...' : '登 录' }}</span>
        </el-button>
      </el-form-item>

      <div class="register-link">
        <span>还没有账号？</span>
        <el-link type="primary" :underline="false" @click="handleRegister">
          立即注册
        </el-link>
      </div>
    </el-form>

    <!-- 底部 -->
    <div class="el-login-footer">
      <span>
        Copyright © 2013-2025
        <a href="#" target="_blank">企业数字化平台管理系统</a>
        版权所有.
      </span>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('@/assets/images/bg.png');
  background-size: cover;

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

    .title {
      margin: 0 auto 30px;
      text-align: center;
      color: #707070;
      font-size: 20px;
      font-weight: bold;
    }

    .el-input {
      height: 40px;
      :deep(.el-input__wrapper) {
        padding: 0 15px;
      }
    }

    .login-options {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 25px;
    }

    .register-link {
      text-align: center;
      margin-top: 20px;
      font-size: 14px;
      color: #606266;
    }
  }

  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;

    a {
      color: white;
      text-decoration: none;

      &:hover {
        text-decoration: underline;
      }
    }
  }
}
</style>